package cn.jolyoulu.common.poi.excel.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.*;
import java.util.zip.ZipInputStream;

/**
 * @Author: JolyouLu
 * @Date: 2025/7/17 10:35
 * @Version 1.0
 * 处理文档嵌入式对象的工具类
 */
@Slf4j
public class OleObjectUtils {

    /**
     * 从OLE中提取相应的文档字节数组
     *
     * @param oleBytes
     * @return 提取出来的字节数组
     */
    private static byte[] extractInnerDoc(byte[] oleBytes) {
        try (POIFSFileSystem pfs = new POIFSFileSystem(new ByteArrayInputStream(oleBytes))) {
            for (Entry entry : pfs.getRoot()) {
                //找到对应的文档对象，把他读取出来
                if (entry.isDocumentEntry()) {
                    try (BufferedInputStream bis = new BufferedInputStream(new DocumentInputStream((DocumentEntry) entry))) {
                        byte[] bytes = new byte[bis.available()];
                        bis.read(bytes);
                        return bytes;
                    }
                }
            }
        } catch (IOException e) {
            log.error("extractInnerDoc",e);
        }
        return null;
    }
}
